
import UIKit
class LeftMenuViewController: UIViewController {
    fileprivate let cellIdentifier = "WLCellIdentifier"
    let headerViewH: CGFloat = 200
    let fooderViewH: CGFloat = 100
    let tableViewH:CGFloat = UIScreen.main.bounds.height - 300
    var dataArray = [["我的发布","发布"],["我的借出","借出"],["我的借入","借入"],["我的收藏","收藏"],["关于亿享","关于"]]
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(img)
        if globalUsername == nil {
            view.addSubview(headView)
        }else{
            view.addSubview(headerView)
        }
        view.addSubview(tableView)
        view.addSubview(fooderView)
    }
    //马上登陆点击事件
    @objc func login(){
        print(globalUsername)
        var vc = UIViewController()
        vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "login")
        vc.view.backgroundColor = UIColor.white
        DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
//        show(vc, sender: Any?.self)
//        globalUsername = "1"
//        view.addSubview(img)
//        if globalUsername == nil {
//            view.addSubview(headView)
//        }else{
//            view.addSubview(headerView)
//        }
//        view.addSubview(tableView)
//        view.addSubview(fooderView)
    }
    //点击 退出按钮
    @objc func exit(){
       globalUsername = nil
        view.addSubview(img)
        if globalUsername == nil {
            view.addSubview(headView)
        }else{
            view.addSubview(headerView)
        }
        view.addSubview(tableView)
        view.addSubview(fooderView)
        print(globalUsername)
    }
    private lazy var img: UIImageView = {
        let img = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        img.image = UIImage(named:"背景")
        let blurEffect = UIBlurEffect(style: .regular)
                //接着创建一个承载模糊效果的视图
                let blurView = UIVisualEffectView(effect: blurEffect)
                //设置模糊视图的大小（全屏）
                blurView.frame.size = CGSize(width: img.frame.width, height: img.frame.height)
                //添加模糊视图到页面view上（模糊视图下方都会有模糊效果）
               img.addSubview(blurView)
        
        return img
    }()
    
    private lazy var tableView: UITableView = {
       
        let tab = UITableView(frame: CGRect(x: 0, y: self.headerViewH, width: UIScreen.main.bounds.width, height: self.tableViewH), style: .plain)
            tab.backgroundColor = UIColor.clear
//        tab.backgroundColor = UIColor(colorLiteralRed: 13.0 / 255.0, green: 184.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
        tab.separatorStyle = UITableViewCellSeparatorStyle.none
        tab.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
        tab.delegate = self
        tab.dataSource = self
        tab.register(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
        return tab
    }()
    
    private lazy var fooderView: UIView = {
    //创建底部View
        
        let food = UIView(frame: CGRect(x: 0, y: self.headerViewH + self.tableViewH, width: UIScreen.main.bounds.width, height: self.headerViewH))
//        food.backgroundColor = UIColor(colorLiteralRed: 13.0 / 255.0, green: 184.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
        food.backgroundColor = UIColor.clear
        //创建设置按钮
        let button:UIButton = UIButton(type:.contactAdd)
        button.frame = CGRect(x:10, y:50, width:100, height:30)
        let iconImage = UIImage(named:"设置")?.withRenderingMode(.alwaysOriginal)
        button.setImage(iconImage, for:.normal)
        button.addTarget(self, action: #selector(sheZhi), for: .touchUpInside)
        food.addSubview(button)
        
        //创建退出登录按钮
        let buttonY:UIButton = UIButton(type:.contactAdd)
        buttonY.frame = CGRect(x:150, y:50, width:100, height:30)
        let iconImageY = UIImage(named:"退出")?.withRenderingMode(.alwaysOriginal)
        buttonY.setImage(iconImageY, for:.normal)
        buttonY.addTarget(self, action: #selector(exit), for: .touchUpInside)
        food.addSubview(buttonY)
        return food
        
    }()
    private lazy var headView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.headerViewH))
        view.backgroundColor = UIColor.clear
        let Uname = UILabel(frame: CGRect(x: 40, y: 60, width: 170, height: 50))
        Uname.text =  "欢迎来到亿享！"
        Uname.textColor = UIColor.black
        Uname.textAlignment = NSTextAlignment.left
        Uname.font = UIFont(name: "PingFangSC-Semibold", size: 20)
        view.addSubview(Uname)
        let buttonY:UIButton = UIButton(type: .system)
        buttonY.frame = CGRect(x: 100, y: 120, width: 150, height: 30)
        buttonY.setTitle("马上登陆", for: .normal)
        buttonY.setTitleColor(UIColor.black, for: .normal)
        buttonY.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        buttonY.backgroundColor = UIColor.init(red: 255/255, green: 102/255, blue: 102/255, alpha: 100)
        buttonY.layer.cornerRadius = 6
        buttonY.addTarget(self, action: #selector(login), for: .touchUpInside)
        view.addSubview(buttonY)
        return view
        
    }()
    private lazy var headerView: UIView = {
        
       
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.headerViewH))
          view.backgroundColor = UIColor.clear
        //创建用户头像
        let Ulogo = UIImageView(image:UIImage(named:"头像"))
        Ulogo.frame = CGRect(x:20, y:50, width:60, height:60)
        let image:CALayer = Ulogo.layer
        image.masksToBounds = true
        image.cornerRadius = Ulogo.frame.size.height / 4
        view.addSubview(Ulogo)
        
        //创建用户名
        let Uname = UILabel(frame: CGRect(x: 100, y: 60, width: 170, height: 30))
        Uname.text =  "阿杰"
        Uname.textColor = UIColor.black
        Uname.textAlignment = NSTextAlignment.left
        Uname.font = UIFont(name: "Uname", size: 15)
        view.addSubview(Uname)
        return view
        
    }()

}

extension LeftMenuViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        cell.backgroundColor = UIColor.clear
        cell.textLabel?.text = dataArray[indexPath.row][0]
        cell.imageView?.image = UIImage(named: dataArray[indexPath.row][1])
        cell.textLabel?.textColor = UIColor.black
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let vc:UIViewController
        switch indexPath.row {
        case 0:
            let alert = UIAlertController(title: "提示", message: "此功能尚未开放", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            break
        case 1:
            
            let alert = UIAlertController(title: "提示", message: "此功能尚未开放", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            
            break
        case 2:
            
            let alert = UIAlertController(title: "提示", message: "此功能尚未开放", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            
            break
        case 3:
            
            let alert = UIAlertController(title: "提示", message: "此功能尚未开放", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            
            break
        case 4:
            vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "iShare")
            vc.view.backgroundColor = UIColor.white
            vc.title = dataArray[indexPath.row][0]
            DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
            break
        default:
            break
        }
    }
    
    //设置按钮单击事件
    @objc func sheZhi() {
        let v:UIViewController
        v = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "set")
        v.view.backgroundColor = UIColor.white
        v.title = "设置"
         DrawerViewController.shareDrawer?.LeftViewController(didSelectController: v)
        
    }
}
